import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

class MyThreadPool
{
    //阻塞队列存放任务
    private  BlockingQueue<Runnable> queue=new LinkedBlockingQueue<>();

    //类似一个生产者
    public void submit(Runnable runnable) throws InterruptedException {
        queue.put(runnable);
    }

    //实现一个固定的线程数的线程池
    //创建固定数的线程
    public MyThreadPool(int n)
    {
        for (int i = 0; i < 10; i++) {
            Thread t1=new Thread( ()-> {

                try {
                    //循环为了不断的取线程
                    while(true)
                    {
                        Runnable runnable=queue.take();
                        runnable.run();
                    }

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
            t1.start();
        }
    }
}
public interface ThreadDemo2 {
    public static void main(String[] args) throws InterruptedException {

        MyThreadPool pool=new MyThreadPool(10);
        for (int i = 0; i < 1000; i++) {
            int j=i;
            pool.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println("hello"+j);
                }
            });

        }
    }
}
